package com.booksystem.campuslibrarymanagementsystem.controller.approve;

import com.booksystem.campuslibrarymanagementsystem.commen.request.approverequest.HistoryViewing.BorrowHistory;
import com.booksystem.campuslibrarymanagementsystem.commen.request.approverequest.HistoryViewing.RenewHistory;
import com.booksystem.campuslibrarymanagementsystem.commen.request.approverequest.HistoryViewing.ReservationHistory;
import com.booksystem.campuslibrarymanagementsystem.commen.request.approverequest.HistoryViewing.ReturnHistory;
import com.booksystem.campuslibrarymanagementsystem.commen.result.Result;
import com.booksystem.campuslibrarymanagementsystem.entity.*;
import com.booksystem.campuslibrarymanagementsystem.service.*;
import com.booksystem.campuslibrarymanagementsystem.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RequestMapping("/approval")
@Api(tags = "审批历史")
@RestController
public class ApprovalHistoryController {


    BorrowApprovalsTable borrowApprovalsTable= new BorrowApprovalsTable();
    RenewalapprovalTable renewalapprovalTable= new RenewalapprovalTable();
    ReturnrequesTable returnrequesTable= new ReturnrequesTable();
    ReservationTable reservationTable=new ReservationTable();

    @Autowired
    ReturnrequesTableService returnrequesTableService;
    @Autowired
    RenewalapprovalTableService renewalapprovalTableService;
    @Autowired
    BorrowApprovalsTableService borrowApprovalsTableService;
    @Autowired
    ReservationTableService reservationTableService;
    @PostMapping("/BorrowingHistoryList")
    @ApiOperation(value = "借阅历史查询", notes = "借阅历史查询")
    public Result BorrowingHistoryList(@RequestBody BorrowHistory borrowHistory) {
        Map<String, Object> parsed = JwtUtil.tokenPre(borrowHistory.getToken());
        if (parsed != null) {
            Object identity = parsed.get("identity");
            if (identity.equals("approval")) {
                String state = borrowApprovalsTable.getApprovalstate();
                if (state == null || state.isEmpty()) {
                    return Result.fail("没有借阅审批已通过的账户信息");
                }

                List<BorrowApprovalsTable> statulist = null; // 调用Service层的querydis方法
                if ("已通过".equals(state)) {
                    statulist = borrowApprovalsTableService.querydislist(state);
                } else if ("未通过".equals(state)) {
                    statulist = borrowApprovalsTableService.querydislist(state);
                } else if ("未审批".equals(state)) {
                    statulist = borrowApprovalsTableService.querydislist(state);
                } else {
                    return Result.fail("无效的状态值");
                }
                // 检查列表是否为空
                if (statulist.isEmpty()) {
                    return Result.fail("没有找到" + "状态为 '" + state + "' 的借阅记录");
                } else {
                    return Result.success(statulist, statulist.size());
                }

            } else {
                return Result.fail("身份权限不足");
            }
        } else {
            return Result.fail("请输入正确token值");
        }
    }

    @PostMapping("/ReservationHistoryList")
    @ApiOperation(value = "预约历史查询", notes = "预约历史查询")
    public Result ResvertionHistoryList(@RequestBody ReservationHistory reservationHistory) {
        Map<String, Object> parsed = JwtUtil.tokenPre(reservationHistory.getToken());
        if (parsed != null) {
            Object identity = parsed.get("identity");
            if (identity.equals("approval")) {
                String state = reservationTable.getStatus();
                if (state == null || state.isEmpty()) {
                    return Result.fail("没有借阅审批已通过的账户信息");
                }

                List<ReservationTable> statulist = null; // 调用Service层的querydis方法
                if ("已通过".equals(state)) {
                    statulist = reservationTableService.querydislist(state);
                } else if ("未通过".equals(state)) {
                    statulist = reservationTableService.querydislist(state);
                } else if ("未审批".equals(state)) {
                    statulist = reservationTableService.querydislist(state);
                } else {
                    return Result.fail("无效的状态值");
                }
                // 检查列表是否为空
                if (statulist.isEmpty()) {
                    return Result.fail("没有找到" + "状态为 '" + state + "' 的预约记录");
                } else {
                    return Result.success(statulist, statulist.size());
                }

            } else {
                return Result.fail("身份权限不足");
            }
        } else {
            return Result.fail("请输入正确token值");
        }
    }
    @PostMapping("/RenewHistoryList")
    @ApiOperation(value = "续借历史查询", notes = "续借历史查询")
    public Result RenewlHistoryList(@RequestBody RenewHistory renewHistory) {
        Map<String, Object> parsed = JwtUtil.tokenPre(renewHistory.getToken());
        if (parsed != null) {
            Object identity = parsed.get("identity");
            if (identity.equals("approval")) {
                String state = renewalapprovalTable.getApprovalstate();
                if (state == null || state.isEmpty()) {
                    return Result.fail("没有借阅审批已通过的账户信息");
                }

                List<RenewalapprovalTable> statulist = null; // 调用Service层的querydis方法
                if ("已通过".equals(state)) {
                    statulist = renewalapprovalTableService.querydislist(state);
                } else if ("未通过".equals(state)) {
                    statulist = renewalapprovalTableService.querydislist(state);
                } else if ("未审批".equals(state)) {
                    statulist = renewalapprovalTableService.querydislist(state);
                } else {
                    return Result.fail("无效的状态值");
                }
                // 检查列表是否为空
                if (statulist.isEmpty()) {
                    return Result.fail("没有找到" + "状态为 '" + state + "' 的续借记录");
                } else {
                    return Result.success(statulist, statulist.size());
                }

            } else {
                return Result.fail("身份权限不足");
            }
        } else {
            return Result.fail("请输入正确token值");
        }
    }

    @PostMapping("/BorrowAppovalHistorylist")
    @ApiOperation(value = "归还审批历史", notes = "归还审批历史")
    public Result ReturnHistoryList(@RequestBody ReturnHistory returnHistory) {
        Map<String, Object> parsed = JwtUtil.tokenPre(returnHistory.getToken());
        if (parsed != null) {
            Object identity = parsed.get("identity");
            if (identity.equals("approval")) {
                String state = returnrequesTable.getApprovalstate();
                if (state == null || state.isEmpty()) {
                    return Result.fail("没有归还审批已通过的账户信息");
                }

                List<ReturnrequesTable> statulist = null; // 调用Service层的querydis方法
                if ("已通过".equals(state)) {
                    statulist = returnrequesTableService.querydislist(state);
                } else if ("未通过".equals(state)) {
                    statulist = returnrequesTableService.querydislist(state);
                } else if ("未审批".equals(state)) {
                    statulist = returnrequesTableService.querydislist(state);
                } else {
                    return Result.fail("无效的状态值");
                }
                // 检查列表是否为空
                if (statulist.isEmpty()) {
                    return Result.fail("没有找到" + "状态为 '" + state + "' 的借阅记录");
                } else {
                    return Result.success(statulist, statulist.size());
                }

            } else {
                return Result.fail("身份权限不足");
            }
        } else {
            return Result.fail("请输入正确token值");
        }
    }



}
